﻿<UserControl x:Class="PocketBook.Views.DesignCatalogView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Themes/generic.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="PageItem" TargetType="{x:Type ListBoxItem}">
                <Setter Property="HorizontalAlignment" Value="Center" />
                <Setter Property="cal:Message.Attach" Value="[Event MouseDoubleClick] = [Action ApplyDesign($dataContext)]"/>
            </Style>

            <Style x:Key="CategoryStyle" TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Expander BorderBrush="Silver" BorderThickness="1" Margin="4,2,4,2" Padding="2">
                                <Expander.Header>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="4"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <Label Grid.Column="1" FontSize="14" FontWeight="Bold" HorizontalAlignment="Left" VerticalContentAlignment="Center" 
                                               Content="{Binding Name}" />
                                    </Grid>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <DataTemplate x:Key="PageDesign">
                <Grid ToolTip="{Binding Path=Design.Description}" HorizontalAlignment="Center">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Border Style="{StaticResource PageSite}">
                        <Viewbox Stretch="Uniform" IsHitTestVisible="false">
                            <ContentControl cal:View.Model="{Binding Design}" Width="{Binding Design.Size.Width}" Height="{Binding Design.Size.Height}" />
                        </Viewbox>
                    </Border>
                    <Label Grid.Row="1" Content="{Binding  Path=Design.DisplayName}" HorizontalAlignment="Center" />
                </Grid>
            </DataTemplate>
        </ResourceDictionary>
    </UserControl.Resources>

    <DockPanel Margin="6">
        <Label DockPanel.Dock="Top" FontWeight="Bold">Templates</Label>
        <Button x:Name="Apply" DockPanel.Dock="Bottom" Margin="0,4,0,0" ToolTip="Apply the selected template to the active page">Apply</Button>

        <ListBox x:Name="Designs" BorderThickness="0" ItemTemplate="{StaticResource PageDesign}" ItemContainerStyle="{StaticResource PageItem}" 
                     SelectedItem="{Binding SelectedItem}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="2" cal:Action.TargetWithoutContext="{Binding}"  />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.GroupStyle>
                <GroupStyle ContainerStyle='{StaticResource CategoryStyle}' />
            </ListBox.GroupStyle>
        </ListBox>
        
    </DockPanel>
</UserControl>
